﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Moon.Common.WebInfo;
using System.IO;
using System.Collections.Generic;
using Moon.Common;
using System.Text;

public partial class Admin_TemplateManage_SystemTemplate : System.Web.UI.UserControl
{
    //服务
    GPI.Server.TemplateService server = new GPI.Server.TemplateService();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            object type = Request.QueryString["Type"];
            if (type != null)
            {
                switch (type.ToString())
                {
                    //如果是系统
                    case "system":
                        BindList("0");
                        this.lbnCreateTemplate.Visible = false;
                        break;
                    case "user":
                        BindList("1");
                        this.lbnCreateTemplate.Visible = true;
                        break;
                }
            }
            
        }
    }


    #region 按钮事件
    //兴建模板
    protected void lbnCreateTemplate_Click(object sender, EventArgs e)
    {
        //切换到第二视图
        this.MultiView1.ActiveViewIndex = 2;
        //清空保存的ID值
        this.btnSaveTemplate.CommandArgument = "0";
    }
    //文件列表动作
    protected void repFileList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        Model.TemplateInfo model = server.GetModel(e.CommandArgument.ObjectToInt(0));
        switch (e.CommandName)
        {
            case "edit":

                this.txtTemplateName.Text = model.TempletaName;
                this.txtContent.Text = new CoXmlHelper().LoadHtml(this.Context, Encoding.UTF8.ToString(),this.Application["WebPath"].ToString()+ model.FilePath).Trim();
                this.btnSaveTemplate.CommandArgument = e.CommandArgument.ToString();
                this.hfFileInfo.Value = model.FilePath;
                this.MultiView1.ActiveViewIndex = 2;
                break;
            case "delete":
                new FileInfo(model.FilePath).Delete();
                if (server.Delete(model.ID))
                    MessageBox.Show(this.Page, "操作成功！");
                else
                    MessageBox.Show(this.Page, "操作失败！");
                break;
        }
    }
    //保存模板
    protected void btnSaveTemplate_Click(object sender, EventArgs e)
    {
        //获取根目录地址
        string root = this.Application["WebPath"].ToString();

        if (!"".Equals(this.btnSaveTemplate.CommandArgument))
        {

            //获取新生成的模板地址
            string path = this.hfFileInfo.Value.IsNullOrEmpty() ? "template\\" + Guid.NewGuid().ToString() + ".htm" :this.hfFileInfo.Value;
            CoFileHepler.DeleteFile(root + path);
            //创建模板
            FileInfo fi = new FileInfo(root + path);
            fi.Create().Close();
            CoXmlHelper cxh = new CoXmlHelper();
            StringBuilder sb = new StringBuilder(this.txtContent.Text.Trim());
            //如果创建文件成功
            bool isCreateTrue = cxh.WriteHtml(this.Context, sb, Encoding.UTF8, fi.FullName);

            if (isCreateTrue)
            {
                Model.TemplateInfo model = new Model.TemplateInfo();
                //文件地址
                model.FilePath = path;
                //模版名称
                model.TempletaName = this.txtTemplateName.Text;
                //模板类型.规定为用户自定义模板
                model.ID = this.btnSaveTemplate.CommandArgument.ToInt(0);
                //如果模型id号为0则为添加
                if (model.ID == 0)
                {
                    model.TempletaType = 1;
                    isCreateTrue = server.Add(model);
                }
                else
                {
                    model.TempletaType = server.GetModel(model.ID).TempletaType;
                    isCreateTrue = server.Update(model);
                }

            }
            else
            {
                CoFileHepler.DeleteFile(fi.FullName);

            }


            if (isCreateTrue)
            {
                MessageBox.Show(this.Page, "操作成功！");
            }
            else
            {
                MessageBox.Show(this.Page, "操作失败！");
            }
        }
        else
        {
            MessageBox.Show(this.Page, "操作失败！");
        }
    }
    //返回信息
    protected void btnReturnList_Click(object sender, EventArgs e)
    {
        this.MultiView1.ActiveViewIndex = 0;
    }
    #endregion

    private void BindList(string id)
    {
        //获得总数
        int count = server.GetRecordCount("TempletaType="+id);


        //切换至列表页
        this.MultiView1.ActiveViewIndex = 0;
        //设置每页信息数
        this.anpFileList.PageSize = 10;
        //信息总数
        this.anpFileList.RecordCount = count;
        //当前索引数
        int _CurrentPageIndex = this.anpFileList.CurrentPageIndex;
        //页面信息数
        int _PageSize = this.anpFileList.PageSize;
        //开始标识
        int starIndex = (_CurrentPageIndex - 1) * _PageSize <= 0 ? 0 : ((_CurrentPageIndex - 1) * _PageSize) + 1;
        // 结束标识
        int endIndex = _CurrentPageIndex * _PageSize;
        //根据标识搜索绑定数据表
        string[] orderstr = { "ID", "DESC" };
        this.repFileList.DataSource = server.GetListByPage(" and TempletaType=" + id, orderstr, starIndex, endIndex);
        this.repFileList.DataBind();
    }



}
